package net.bw.realtime.tmall.dwd.function;

/*
 * @ Author：liuyawei
 * @ Date：2025-05-27
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bw.realtime.tmall.dwd.bean.TransactionEventBean;
import org.apache.flink.api.common.functions.RichMapFunction;

public class TransactionEventBeanMapFunction extends RichMapFunction<String, TransactionEventBean> {

    @Override
    public TransactionEventBean map(String value) throws Exception {

        // 解析数据
        JSONObject jsonObject = JSON.parseObject(value);

        JSONObject actionDetail = jsonObject.getJSONObject("actionDetail");
        JSONObject deviceInfo = jsonObject.getJSONObject("deviceInfo");
        JSONObject location = jsonObject.getJSONObject("location");
        JSONObject session = jsonObject.getJSONObject("session");

        String orderID = jsonObject.getString("orderId");
        String userID = jsonObject.getString("userId");
        String skuID = actionDetail.getString("skuId");
        String skuName = actionDetail.getString("sku_name");
        String shopID = actionDetail.getString("shop_id");
        String shopName = actionDetail.getString("shop_name");
        String deviceType = deviceInfo.getString("deviceType");
        String category = actionDetail.getString("category");
        String brand = actionDetail.getString("brand");
        String province = location.getString("province");
        String city = location.getString("city");
        String sessionID = session.getString("sessionId");
        String actionType = jsonObject.getString("actionType");
        Double price = actionDetail.getDouble("price");
        Integer quantity = actionDetail.getInteger("quantity");
        Long ts = jsonObject.getLong("ts");

        TransactionEventBean bean = new TransactionEventBean(
                orderID, userID, skuID, skuName, shopID, shopName, deviceType,
                category, brand, province, city, sessionID, actionType, price, quantity, ts
        );

        return bean;
    }
}
